package com.whsxt.common.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.whsxt.common.CacheBean;
import com.whsxt.common.CachePool;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;

@RequestMapping("/cache")
@RestController
public class CacheController {
	
	/**
	 * 	 显示所有缓存
	 * @return
	 */
	@RequestMapping("/list")
	public Object list() {
		Map<String, Object> map = CachePool.map;
		List<CacheBean> list = new ArrayList<CacheBean>();
		Set<Entry<String, Object>> entrySet = map.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			list.add(new CacheBean(entry.getKey(), entry.getValue()));
		}
		PageInfo pageInfo = new PageInfo(10000L, list);
		return new Result(pageInfo);
	}
	
	/**
	 * 	根据key 删除缓存
	 * @param key
	 * @return
	 */
	@RequestMapping("/removeCacheByKey")
	public Object removeCacheByKey(String key) {
		CachePool.removeCacheByKey(key);
		return new Result(CodeMsg.CACHE_DELETE_SUCCESS);
	}
	
	/**
	 * 	删除全部缓存 
	 * @return
	 */
	@RequestMapping("/removeAll")
	public Object removeAll() {
		CachePool.removeAll();
		return new Result(CodeMsg.CACHE_DELETE_SUCCESS);
	}
	
	/**
	 * 	同步所有缓存
	 * @return
	 */
	@RequestMapping("/syncAll")
	public Object syncAll() {
		CachePool.syncAll();
		return new Result(CodeMsg.CACHE_SYNC_SUCCESS);
	}
	
}
